package com.utilities.parsers.aceParser;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

import com.utilities.parsers.aceParser.ParseEvents.EventTypes;

public class ParseAllContigsFileParser   
{
	@SuppressWarnings("unused")
	private String stateString = null;
	private EventTypes type = null;
	
	private LinkedList<ParseListener> eventListeners = new LinkedList<ParseListener>();
	
	public void addListener(ParseListener parseListener)
	{
		eventListeners.add(parseListener);
	}
	
	private void fireEvent(ParseEvents parseEvent)
	{
		for (ParseListener listener : eventListeners)
		{
			listener.handleEvent(parseEvent);
		}
	}
	
	public void parse(FileReader fileReader) throws IOException
	{
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		fireEvent(new ParseEvents(null,EventTypes.docStarted));
		String readLine = bufferedReader.readLine();
		stateString = readLine;
		while(readLine != null)
		{
			if (readLine.startsWith("CO"))
			{
				type = EventTypes.contigIdentifier;
			}
			else if (readLine.startsWith("BQ"))
			{
				type = EventTypes.BQ;
			}
			else if (readLine.startsWith("AF"))
			{
				type = EventTypes.AFLine;
			}
			else if (readLine.startsWith("BS"))
			{
				type = EventTypes.BSLine;
			}
			else if (readLine.startsWith("RD"))
			{
				type = EventTypes.RDLine;
			}
			else if (readLine.startsWith("QA"))
			{
				type = EventTypes.QA;
			}
			else if (type == EventTypes.contigIdentifier || type == EventTypes.contigSeqRead)
			{
				type = EventTypes.contigSeqRead;
			}
			else if (type == EventTypes.BQ || type == EventTypes.BQLineRead)
			{
				type = EventTypes.BQLineRead;
			}
			else if (type == EventTypes.RDLine || type == EventTypes.RDSeq)
			{
				type = EventTypes.RDSeq;
			}
			
			if (!readLine.trim().equalsIgnoreCase("") )
			readLine = bufferedReader.readLine();
		}
		fireEvent(new ParseEvents(null,EventTypes.docEnded));
	}
}
